package cn.anlucky.apiadmin.controller;

import cn.anlucky.apiadmin.dto.PageDto;
import cn.anlucky.apiadmin.pojo.Apiinfo;
import cn.anlucky.apiadmin.service.ApiinfoService;
import cn.anlucky.apiadmin.vo.R;
import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api-manager")
public class ApiManagerController {

    @Autowired
    private ApiinfoService apiinfoService;

    /**
     * 首页取API列表数据
     * @return
     */
    @SaIgnore// 游客可以访问
    @GetMapping("/getApiInfoByIndex")
    public R getApiInfoByIndex(){
        R r = apiinfoService.getApiInfoList();
        return r;
    }



    /**
     * 获取Api列表
     * @return
     */
    @GetMapping("/getApiInfoList")
    public R getApiInfoList(){
        List<Apiinfo> list = apiinfoService.list();
        return R.success(list);
    }

    /**
     * 分页获取Api列表
     * @return
     */
    @PostMapping("/getApiInfoListByPage")
    public R getApiInfoListByPage(@RequestBody PageDto pageDto){
        R r = apiinfoService.selectApiInfoByPage(pageDto);
        return r;
    }

    /**
     * ID取API信息
     * @param id
     * @return
     */
    @SaIgnore // 游客访问
    @GetMapping("/getApiInfoById/{id}")
    public R getApiInfoById(@PathVariable Integer id){
        Apiinfo apiinfo = apiinfoService.getById(id);
        return R.success(apiinfo);
    }

    /**
     * 增加一个API
     * @param dateForm
     * @return
     */
    @PostMapping("/insertApiInfo")
    public R insertApiInfo(@RequestBody Map<String,Object> dateForm){
        System.out.println("dateForm = " + dateForm);
        R r = apiinfoService.insertApiInfo(dateForm);
        return r;
    }

    /**
     * 更新一个API
     * @param dateForm
     * @return
     */
    @PostMapping("/updateApiInfoById")
    public R updateApiInfoById(@RequestBody Map<String,Object> dateForm){
        R r = apiinfoService.updateApiInfoById(dateForm);
        return r;
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deleteAPIByIds")
        public R deleteAPIByIds(@RequestBody List<Long> ids){
        System.out.println("ids = " + ids);
        R r = apiinfoService.deleteAPIByIds(ids);
        return r;
    }

    /**
     * 批量启用
     * @param ids
     * @return
     */
    @PostMapping("/enabledApiByIds")
    public R enabledApiByIds(@RequestBody List<Long> ids){
        System.out.println("ids = " + ids);
        R r = apiinfoService.enabledAPIByIds(ids);
        return r;
    }

    /**
     * 批量禁用
     * @param ids
     * @return
     */
    @PostMapping("/disabledApiByIds")
    public R disabledApiByIds(@RequestBody List<Long> ids){
        System.out.println("ids = " + ids);
        R r = apiinfoService.disabledAPIByIds(ids);
        return r;
    }



}
